﻿using UnityEngine;

namespace CGF
{
    public class TurnPage_Method_Top : TurnPage_Method
    {
        public TurnPage_Method_Top(TurnPage turnPage) : base(turnPage, Side.Top) { }

        public override void SetTargetsPivot(Vector2 touchPos)
        {
            main.mask.pivot = new Vector2(0, 0.5f);
            main.contentClone.pivot = new Vector2(1 - (touchPos.x - main.bottomLeft.x) / main.cardWidth, 1);
            if (main.IsTransverse)
            {
                main.contentClone.pivot = new Vector2(0, 1 - (touchPos.x - main.bottomLeft.x) / main.cardWidth);
            }
            main.symmetryPoint = new Vector2(touchPos.x, main.topLeft.y);
            Update();
        }

        public override void Update()
        {
            switch (currAnimState)
            {
                case AnimState.Drag:
                    Vector2 touchPos = main.hadDragTouchDown ? main.dragPos : main._TouchPos();
                    dragPos = touchPos;
                    if (dragPos.y > main.topLeft.y - 1)
                    {
                        dragPos.y = main.topLeft.y - 1;
                    }
                    bool canLimitBottomLeft = false;
                    if (touchPos.y > main.bottomLeft.y)
                    {
                        canLimitBottomLeft = (touchPos.x < main.bottomLeft.x);
                    }
                    else
                    {
                        canLimitBottomLeft = (touchPos.x < main.bottomLeft.x + main.cardWidth * 0.5f);
                    }
                    if (canLimitBottomLeft)
                    {
                        if (Vector2.Distance(touchPos, main.bottomLeft) > (main.cardHeight - 50))
                        {
                            float r = Mathf.Atan2(touchPos.y - main.bottomLeft.y, touchPos.x - main.bottomLeft.x);
                            dragPos.x = main.bottomLeft.x + Mathf.Cos(r) * (main.cardHeight - 50);
                            dragPos.y = main.bottomLeft.y + Mathf.Sin(r) * (main.cardHeight - 50);
                        }
                    }
                    bool canLimitBottomRight = false;
                    if (touchPos.y > main.bottomLeft.y)
                    {
                        canLimitBottomRight = (touchPos.x > main.bottomRight.x);
                    }
                    else
                    {
                        canLimitBottomRight = (touchPos.x > main.bottomRight.x - main.cardWidth * 0.5f);
                    }
                    if (canLimitBottomRight)
                    {
                        if (Vector2.Distance(touchPos, main.bottomRight) > (main.cardHeight - 50))
                        {
                            float r = Mathf.Atan2(touchPos.y - main.bottomRight.y, touchPos.x - main.bottomRight.x);
                            dragPos.x = main.bottomRight.x + Mathf.Cos(r) * (main.cardHeight - 50);
                            dragPos.y = main.bottomRight.y + Mathf.Sin(r) * (main.cardHeight - 50);
                        }
                    }
                    break;
                case AnimState.Resume:
                    dragPos = Vector2.MoveTowards(dragPos, main.symmetryPoint, main.cardWidth * 8f * Time.deltaTime);
                    if (Vector2.Distance(dragPos, main.symmetryPoint) == 0)
                    {
                        main.Resume();
                    }
                    break;
            }
            if (currAnimState != AnimState.TurnOver)
            {
                Vector2 centerLinePoint = GetCenterBetween2Points(dragPos, main.symmetryPoint);
                float centerLineRadian = Mathf.Atan2(main.symmetryPoint.y - dragPos.y, main.symmetryPoint.x - dragPos.x) - Mathf.PI * 0.5f;
                Line centerLine = new Line(centerLinePoint, centerLineRadian);
                leftPoint = GetCrossPointOf2Line(centerLine, main.lineL);
                rightPoint = GetCrossPointOf2Line(centerLine, main.lineR);

                if (leftPoint.y >= main.topLeft.y && rightPoint.y < main.topRight.y)
                {
                    leftPoint = GetCrossPointOf2Line(centerLine, main.lineT);
                }
                else if (leftPoint.y < main.topLeft.y && rightPoint.y >= main.topRight.y)
                {
                    rightPoint = GetCrossPointOf2Line(centerLine, main.lineT);
                }

                if (leftPoint.y <= main.bottomLeft.y && rightPoint.y > main.bottomRight.y)
                {
                    leftPoint = GetCrossPointOf2Line(centerLine, main.lineB);
                }
                else if (leftPoint.y > main.bottomLeft.y && rightPoint.y <= main.bottomRight.y)
                {
                    rightPoint = GetCrossPointOf2Line(centerLine, main.lineB);
                }

                if (main.mask)
                {
                    Vector2 maskPos = leftPoint;
                    maskPos += main.offset;
                    main.mask.anchoredPosition = maskPos;
                    main.mask.localEulerAngles = new Vector3(0, 0, Mathf.Atan2(rightPoint.y - leftPoint.y, rightPoint.x - leftPoint.x) * Mathf.Rad2Deg - 90);
                }
                if (main.backClone)
                {
                    main.backClone.SetParent(main.rectTransform.parent);
                    main.backClone.anchoredPosition = main.rectTransform.anchoredPosition;
                    main.backClone.localEulerAngles = main.rectTransform.localEulerAngles;
                    main.backClone.SetParent(main.mask, true);
                }
                if (main.contentClone)
                {
                    main.contentClone.SetParent(main.rectTransform.parent);
                    Vector2 contentClonePos = dragPos;
                    contentClonePos += main.offset;
                    main.contentClone.anchoredPosition = contentClonePos;
                    main.contentClone.localEulerAngles = new Vector3(0, 0, main.IsTransverse ? centerLine.radian * Mathf.Rad2Deg * 2f + 90 : centerLine.radian * Mathf.Rad2Deg * 2f + 180);
                    main.contentClone.SetParent(main.mask, true);
                }
                if (main.shadow)
                {
                    main.shadow.SetParent(main.rectTransform.parent);
                    Vector2 shadowPos = GetCenterBetween2Points(leftPoint, rightPoint);
                    main.shadow.anchoredPosition = shadowPos;
                    main.shadow.localEulerAngles = new Vector3(0, 0, centerLine.radian * Mathf.Rad2Deg);
                    main.shadow.SetParent(main.contentClone, true);
                }
                bool canStop = false;
                if (main.hadDragTouchDown)
                {
                    canStop = !main.isDraging;
                }
                else
                {
                    canStop = Input.GetMouseButtonUp(0);
                }
                if (canStop)
                {
                    if (dragPos.y < main.bottomLeft.y)
                    {
                        main.contentClone.SetParent(main.rectTransform.parent, true);
                        //将锚点移回中心并确保卡片位置不变
                        ChangePivotToCenter(main.contentClone, dragPos, main.IsTransverse);
                        if (main.mask)
                        {
                            GameObject.Destroy(main.mask.gameObject);
                        }
                        if (main.shadow)
                        {
                            GameObject.Destroy(main.shadow.gameObject);
                        }
                        currAnimState = AnimState.TurnOver;
                        SoundPlayer.Play(main.dragSound);
                    }
                    else
                    {
                        currAnimState = AnimState.Resume;
                    }
                    if (main.onTouchUpCurl != null)
                    {
                        main.onTouchUpCurl();
                    }
                    main.isCurling = false;
                    main.hadDragTouchDown = false;
                }
            }
            else
            {
                Vector3 toAngle = new Vector3(0, 0, main.IsTransverse ? 90 : 0);
                main.contentClone.localRotation = Quaternion.Lerp(main.contentClone.localRotation, Quaternion.Euler(toAngle), main.cardWidth * turnOverSpeed * Time.deltaTime);
                if (Vector2.Distance(main.contentClone.anchoredPosition, main.rectTransform.anchoredPosition) > 0.1f)
                {
                    main.contentClone.anchoredPosition = Vector2.Lerp(main.contentClone.anchoredPosition, main.rectTransform.anchoredPosition, main.cardWidth * turnOverSpeed * Time.deltaTime);
                }
                else
                {
                    main.TurnOverFinish();
                }
            }
        }

        /// <summary>
        /// 将锚点移回中心并确保卡片位置不变
        /// </summary>
        void ChangePivotToCenter(RectTransform target, Vector2 currDragPos, bool isTransverse)
        {
            if (!isTransverse)
            {
                Vector2 pivotPos = currDragPos;
                float r = target.localEulerAngles.z * Mathf.Deg2Rad;
                float l = (target.pivot.x - 0.5f) * main.cardWidth;
                pivotPos.x -= Mathf.Cos(r) * l;
                pivotPos.y -= Mathf.Sin(r) * l;
                r += Mathf.PI * 0.5f;
                l = main.cardHeight * 0.5f;
                pivotPos.x -= Mathf.Cos(r) * l;
                pivotPos.y -= Mathf.Sin(r) * l;
                target.pivot = new Vector2(0.5f, 0.5f);
                target.anchoredPosition = pivotPos;
                target.localEulerAngles += new Vector3(0, 0, 180);
            }
            else
            {
                Vector2 pivotPos = currDragPos;
                float r = (target.localEulerAngles.z + 90) * Mathf.Deg2Rad;
                float l = (target.pivot.y - 0.5f) * main.cardWidth;
                pivotPos.x -= Mathf.Cos(r) * l;
                pivotPos.y -= Mathf.Sin(r) * l;
                r += Mathf.PI * 0.5f;
                l = main.cardHeight * 0.5f;
                pivotPos.x -= Mathf.Cos(r) * l;
                pivotPos.y -= Mathf.Sin(r) * l;
                target.pivot = new Vector2(0.5f, 0.5f);
                target.anchoredPosition = pivotPos;
            }
        }
    }
}